package com.taotao.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUiDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.CommonUtil;
import com.taotao.common.util.JsonUtils;
import com.taotao.content.jedis.JedisClient;
import com.taotao.content.service.ContentService;
import com.taotao.manager.mapper.TbContentMapper;
import com.taotao.manager.pojo.TbContent;
import com.taotao.manager.pojo.TbContentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper contentMapper;
    @Autowired
    private JedisClient jedis;
    private String CONTENT_KEY="CONTENT_KEY";
    @Override
    public EasyUiDataGridResult getContentList(Integer page, Integer rows, Long categoryId) {
        //注入mapper
        //创建example
        //3.设置查询条件
        //执行查询
        //转成EasyUiTreeNode
        //返回

        PageHelper.startPage(page, rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = contentMapper.selectByExample(example);
//        List<TbContent> list = getContentListByCategoryId(categoryId);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return result;
    }

    @Override
    public List<TbContent> getContentListByCategoryId(Long categoryId) {
        try {
            String jsonStr = jedis.hget(CONTENT_KEY, categoryId + "");
            //如果存在，说明有缓存
            if (!StringUtils.isEmpty(jsonStr)) {
                System.out.println("从redis缓存中取内容列表数据。。。");
                return JsonUtils.jsonToList(jsonStr, TbContent.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = contentMapper.selectByExample(example);

        //数据写入redis中
        try {
            jedis.hset(CONTENT_KEY, categoryId + "", JsonUtils.objectToJson(list));
        }catch (Exception e){
            e.printStackTrace();
        }


        return list;
    }

    @Override
    public TaotaoResult saveContent(TbContent content) {
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);
        contentMapper.insertSelective(content);
        //当添加内容的时候，需要清空此内容所属分类下的缓存
        try {
            jedis.hdel(CONTENT_KEY, content.getCategoryId() + "");
            System.out.println("新增内容时清空缓存！！！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult editContent(TbContent content) {
        TbContent content1 = contentMapper.selectByPrimaryKey(content.getId());
        CommonUtil.copyPropertiesIgnoreNull(content,content1);
        contentMapper.updateByPrimaryKey(content1);
        try {
            jedis.hdel(CONTENT_KEY, content1.getCategoryId() + "");
            System.out.println("修改内容时清空缓存！！！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteBatchContent(String ids) {
        if(!StringUtils.isEmpty(ids)){
            String[] idss = ids.split(",");
            for(String idStr:idss){
                Long id = Long.parseLong(idStr);
                TbContent content = contentMapper.selectByPrimaryKey(id);
                contentMapper.deleteByPrimaryKey(id);
                try {
                    jedis.hdel(CONTENT_KEY, content.getCategoryId() + "");
                    System.out.println("删除内容时清空缓存！！！");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return TaotaoResult.ok();
    }
}
